package com.atguigu.controller;

import com.atguigu.entity.*;
import com.atguigu.entity.bo.HouseQueryBo;
import com.atguigu.entity.vo.HouseVo;
import com.atguigu.result.Result;
import com.atguigu.service.*;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Date:2022/11/5
 * Author:ybc
 * Description:
 */
@RestController
@RequestMapping("/house")
public class HouseController {

    @DubboReference
    private HouseService houseService;
    @DubboReference
    private CommunityService communityService;
    @DubboReference
    private HouseBrokerService houseBrokerService;
    @DubboReference
    private HouseImageService houseImageService;
    @DubboReference
    private UserFollowService userFollowService;
    @RequestMapping("/list/{pageNum}/{pageSize}")
    public Result list(@PathVariable Integer pageNum, @PathVariable Integer pageSize, @RequestBody HouseQueryBo houseQueryBo){
        PageInfo<HouseVo> page = houseService.findHouseVoPage(pageNum, pageSize, houseQueryBo);
        return Result.ok(page);
    }


    @RequestMapping("/info/{houseId}")
    public Result info(@PathVariable("houseId") Long houseId, HttpSession session){

        //查询房源信息
        House house = houseService.getById(houseId);
        //查询房源所对应的小区信息
        Community community = communityService.getById(house.getCommunityId());
        //查询房源所对应的经纪人信息
        List<HouseBroker> houseBrokerList = houseBrokerService.findHouseBrokerListByHouseId(houseId);
        //查询房源所对应的图片信息
        List<HouseImage> houseImage1List = houseImageService.findHouseImageListByHouseIdAndType(houseId,1);
        Map<String, Object> map = new HashMap<>();
        map.put("house",house);
        map.put("community",community);
        map.put("houseBrokerList",houseBrokerList);
        map.put("houseImage1List",houseImage1List);
        //是否关注
        boolean isFollow = false;
        //获取当前登录的用户信息
        UserInfo userInfo = (UserInfo) session.getAttribute("USER");
        if(userInfo != null){
            //已登录，判断当前用户是否已经关注了该房源
            Long userId = userInfo.getId();
            //根据用户id和房源id查询UserFollow
            UserFollow userFollow = userFollowService.isFollow(userId, houseId);
            isFollow = userFollow != null;
        }
        map.put("isFollow",isFollow);
        return Result.ok(map);
    }

}
